package com.sunelec.service.Imp;

import com.sun.javaws.security.AppContextUtil;
import com.sun.xml.internal.bind.v2.model.core.ID;
import com.sunelec.mapper.PowerMapper;
import com.sunelec.mapper.Relationship2Mapper;
import com.sunelec.mapper.RelationshipMapper;
import com.sunelec.mapper.UsersMapper;
import com.sunelec.pojo.*;
import com.sunelec.pojo.UsersExample.Criteria;
import com.sunelec.service.UserService;
import com.sunelec.utils.IDUtils;
import com.sunelec.utils.SessionListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.web.context.support.WebApplicationContextUtils;
import sun.security.provider.Sun;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;

/**
 * @Author: 郑明利
 * @Description:用户管理Service
 * @Date: Create in 13:29 2018/04/17
 * @Modified By:
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private PowerMapper powerMapper;

    @Autowired
    private RelationshipMapper relationshipMapper;

    @Autowired
    private Relationship2Mapper relationship2Mapper;

    /**
     * 唯一性校验
     * @param content
     * @param type
     * @return
     */
    @Override
    public SunElecResult checkData(String content, Integer type) {
        //创建查询条件
        UsersExample example = new UsersExample();
        Criteria criteria = example.createCriteria();
        //对数据进行检验:1,2,3分别代表username，phone，email
        if(type == 1) {
            //用户名校验
            criteria.andUsersnameEqualTo(content);
        }else if (type == 2) {
            //电话校验
            criteria.andPhoneEqualTo(content);
        }
        //执行查询
        List<Users> list = usersMapper.selectByExample(example);
        if(list == null || list.size() == 0) {
            return SunElecResult.ok(true);
        }

        if(type == 1) {
            //用户名
            return SunElecResult.build(400, "用户名重复");
        }else if (type == 2) {
            //电话
            return SunElecResult.build(400, "电话重复");
        }
        return SunElecResult.ok(false);
    }


    /**
     * 用户登录
     * <p>Title: userLogin</p>
     * <p>Description: </p>
     * @param username
     * @param password
     * @return
     */
    @Override
    public SunElecResult userLogin(String username, String password, HttpServletRequest request, HttpServletResponse response) {
        UsersExample users = new UsersExample();
        UsersExample.Criteria criteria = users.createCriteria();
        criteria.andUsersnameEqualTo(username);
        List<Users> list = usersMapper.selectByExample(users);
        //如果没有此用户名
        if (null == list || list.size() == 0) {
            return SunElecResult.build(400, "用户名或密码错误");
        }
        Users user = list.get(0);
        //比对密码
        if (!password.equals(user.getPassword())) {
            return SunElecResult.build(400, "用户名或密码错误");
        }
        return SunElecResult.ok();
    }

    /**
     * 用户注册
     * @param users
     * @return
     */
    @Override
    public SunElecResult createUser(Users users){
        usersMapper.insertSelective(users);
        return SunElecResult.ok();
    }

    /**
     * 增加电站信息
     * @param power
     * @return
     */
    @Override
    public SunElecResult createPower(Power power,String username){
        powerMapper.insert(power);
        Relationship relationship = new Relationship();
        relationship.setRelationId("Relationship_" + IDUtils.getId());
        relationship.setLevel3user3(username);
        relationship.setPowerId(power.getPowerId());
        if(relationshipMapper.insert(relationship)==1){
            return SunElecResult.ok();
        }else {
            return SunElecResult.build(400,"添加失败");
        }
    }

    /**
     * 添加用户与电站之间的约束条件
     * @param powerId
     * @param username
     * @return
     */
    @Override
    public SunElecResult makeRelationship(String powerId, String username) {
        String id = "R_"+IDUtils.getId();
        Relationship relationship = new Relationship();
        relationship.setPowerId(powerId);
        relationship.setLevel3user3(username);
        relationship.setRelationId(id);
        if(relationshipMapper.insertSelective(relationship)==1){
            return SunElecResult.ok();
        }else {
            return SunElecResult.build(400,"添加失败");
        }
    }

    /**
     * 添加采集器与电站之间的约束条件
     * @param powerId
     * @param collectId
     * @return
     */
    @Override
    public SunElecResult makeRelationship2(String powerId, String collectId) {
        String id = "R2_"+IDUtils.getId();
        Relationship2 relationship2 = new Relationship2();
        relationship2.setPowerId(powerId);
        relationship2.setCollectorId(collectId);
        relationship2.setId(id);
        if(relationship2Mapper.insertSelective(relationship2)==1){
            return SunElecResult.ok();
        }else {
            return SunElecResult.build(400,"添加失败");
        }
    }
}
